package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private CategoryFeignClient categoryFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private RedissonClient redissonClient;
    @Override
    public Map<String, Object> getItem(Long albumId) {

        HashMap<String, Object> map = new HashMap<>();

        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if (!bloomFilter.contains(albumId)){
            return map;
        }

        CompletableFuture<AlbumInfo> albumInfoCompletablefuture = CompletableFuture.supplyAsync(()->{
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult,"远程调用获取专辑信息失败");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo,"远程调用获取专辑信息失败");

            map.put("albumInfo",albumInfo);

            return albumInfo;
        }, threadPoolExecutor).exceptionally(ex->{
            System.out.println(ex.getMessage());
            return new AlbumInfo();
        });
        CompletableFuture<Integer> categoryCompletableFuture = albumInfoCompletablefuture.thenApplyAsync(albumInfo -> {
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(categoryViewResult, "远程调用获取分类视图失败");
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            Assert.notNull(baseCategoryView, "远程调用分类视图失败");
            map.put("baseCategoryView", baseCategoryView);
            return 1;
        },threadPoolExecutor).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });
        CompletableFuture<Integer> userCompletableFuture = albumInfoCompletablefuture.thenApplyAsync(albumInfo -> {
            Result<UserInfoVo> infoVoResult = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            Assert.notNull(infoVoResult, "远程调用获取用户信息失败");
            UserInfoVo userInfoVo = infoVoResult.getData();
            Assert.notNull(userInfoVo, "远程调用获取用户消息失败");
            map.put("announcer", userInfoVo);
            return 1;
        },threadPoolExecutor).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });
        CompletableFuture<Integer> statCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<AlbumStatVo> statVoResult = albumInfoFeignClient.getAlbumInfoStaVo(albumId);
            Assert.notNull(statVoResult, "远程调用获取专辑统计信息失败");
            AlbumStatVo albumStatVo = statVoResult.getData();
            Assert.notNull(albumStatVo, "远程调用获取专辑统计信息失败");
            map.put("albumStat", albumStatVo);
            return 1;
        },threadPoolExecutor).exceptionally(ex -> {
            System.out.println(ex.getMessage());
            return 0;
        });
        try {
            if(null == albumInfoCompletablefuture.get()|| 0 ==categoryCompletableFuture.get()
                    || 0 ==userCompletableFuture.get() || 0==statCompletableFuture.get()){


                return map;
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        CompletableFuture.allOf(
                albumInfoCompletablefuture,
                categoryCompletableFuture,
                userCompletableFuture,
                statCompletableFuture
                ).join();



        return map;
    }
}
